package org.csu.agrisupply.api.controller.front;

import org.csu.agrisupply.api.common.CommonResponse;
import org.csu.agrisupply.api.entity.FormOffer;
import org.csu.agrisupply.api.entity.FormOrder;
import org.csu.agrisupply.api.entity.User;
import org.csu.agrisupply.api.service.FormOfferService;
import org.csu.agrisupply.api.vo.FormOfferVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/formOffers/")
public class FormOfferController {
    @Autowired
    private FormOfferService formOfferService;

    @GetMapping("")
    @ResponseBody
    //得到所有报价单
    public CommonResponse<List<FormOffer>> getFormOfferList() {
        return formOfferService.getFormOfferList();
    }

    ;

    @GetMapping("forms/{formId}/users/{userId}")
    @ResponseBody
    //得到单个报价单
    public CommonResponse<FormOffer> getFormOffer(@PathVariable("formId") String formId,
                                                  @PathVariable("userId") String userId) {
        return formOfferService.getFormOffer(formId, userId);
    }

    ;

    @PostMapping("forms/{formId}/users/{userId}")
    @ResponseBody
    //进行报价
    public CommonResponse<FormOffer> createOffer(@PathVariable("formId") String formId,
                                                 @PathVariable("userId") String userId,
                                                 @RequestParam(value = "price", required = true) float price,
                                                 @RequestParam(value = "count", required = true) int count,
                                                 @RequestParam(value = "fromAddr", required = true) String fromAddr) {
        return formOfferService.createOffer(formId, userId, price, count, fromAddr);
    }

    @PutMapping("forms/{formId}/users/{userId}")
    @ResponseBody
    //修改报价单信息
    public CommonResponse<FormOffer> updateOffer(@PathVariable("formId") String formId,
                                                 @PathVariable("userId") String userId,
                                                 @RequestParam(value = "price", required = false,defaultValue = "0") float price,
                                                 @RequestParam(value = "count", required = false,defaultValue = "0") int count,
                                                 @RequestParam(value = "fromAddr", required = false) String fromAddr,
                                                 @RequestParam(value = "status" , required = false,defaultValue = "4") int status) {
        return formOfferService.updateOffer(formId, userId, price, count, fromAddr,status);
    }

    @GetMapping("users/{userId}")
    @ResponseBody
    //根据userId得到所有报价单
    public CommonResponse<List<FormOfferVo>> getFormOfferListbyUserId(@PathVariable("userId") String userId) {
        return formOfferService.getFormOfferListbyUserId(userId);
    }

    @GetMapping("forms/{formId}")
    @ResponseBody
    //根据formId得到所有报价单
    public CommonResponse<List<FormOfferVo>> getFormOfferListbyFormId(@PathVariable("formId") String formId) {
        return formOfferService.getFormOfferListbyFormId(formId);
    }

    @DeleteMapping("forms/{formId}/users/{userId}")
    @ResponseBody
    //取消对应offer
    public CommonResponse<FormOfferVo> deleteOffer(@PathVariable("formId") String formId,
                                                   @PathVariable("userId") String userId) {
        return formOfferService.deleteOffer(formId,userId);
    }

}
